package com.buaa.divinationmaster.core.database.entities;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

/**
 * 占卜历史记录实体类
 * 存储用户的所有占卜记录，支持收藏、搜索、分类等功能
 */
@Entity(tableName = "divination_history")
public class DivinationHistory {
    
    @PrimaryKey(autoGenerate = true)
    public long id;
    
    @ColumnInfo(name = "type")
    public String type;               // 占卜类型: tarot/plumblossom/coinoracle/lenormand
    
    @ColumnInfo(name = "question")
    public String question;           // 占卜问题
    
    @ColumnInfo(name = "result_data")
    public String resultData;         // 结果数据 (JSON格式存储)
    
    @ColumnInfo(name = "interpretation")
    public String interpretation;     // 解读文本
    
    @ColumnInfo(name = "timestamp")
    public long timestamp;            // 占卜时间戳
    
    @ColumnInfo(name = "is_favorite")
    public boolean isFavorite;        // 是否收藏
    
    @ColumnInfo(name = "tags")
    public String tags;               // 标签 (逗号分隔: 感情,事业,健康等)
    
    @ColumnInfo(name = "user_rating")
    public Integer userRating;        // 用户评分 (1-5星)
    
    @ColumnInfo(name = "notes")
    public String notes;              // 用户备注
    
    @ColumnInfo(name = "accuracy_feedback")
    public Integer accuracyFeedback;  // 准确度反馈 (1-5)
    
    @ColumnInfo(name = "created_date")
    public String createdDate;        // 创建日期 (YYYY-MM-DD格式，便于分组)
    
    // 默认构造函数
    public DivinationHistory() {}
    
    // 带参构造函数 - Room忽略此构造函数
    @Ignore
    public DivinationHistory(String type, String question, String resultData, String interpretation, long timestamp) {
        this.type = type;
        this.question = question;
        this.resultData = resultData;
        this.interpretation = interpretation;
        this.timestamp = timestamp;
        this.isFavorite = false;
        
        // 自动生成创建日期
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        this.createdDate = sdf.format(new java.util.Date(timestamp));
    }
    
    // Getter和Setter方法
    public long getId() { return id; }
    public void setId(long id) { this.id = id; }
    
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    
    public String getQuestion() { return question; }
    public void setQuestion(String question) { this.question = question; }
    
    public String getResultData() { return resultData; }
    public void setResultData(String resultData) { this.resultData = resultData; }
    
    public String getInterpretation() { return interpretation; }
    public void setInterpretation(String interpretation) { this.interpretation = interpretation; }
    
    public long getTimestamp() { return timestamp; }
    public void setTimestamp(long timestamp) { 
        this.timestamp = timestamp;
        // 更新创建日期
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
        this.createdDate = sdf.format(new java.util.Date(timestamp));
    }
    
    public boolean isFavorite() { return isFavorite; }
    public void setFavorite(boolean favorite) { isFavorite = favorite; }
    
    public String getTags() { return tags; }
    public void setTags(String tags) { this.tags = tags; }
    
    public Integer getUserRating() { return userRating; }
    public void setUserRating(Integer userRating) { this.userRating = userRating; }
    
    public String getNotes() { return notes; }
    public void setNotes(String notes) { this.notes = notes; }
    
    public Integer getAccuracyFeedback() { return accuracyFeedback; }
    public void setAccuracyFeedback(Integer accuracyFeedback) { this.accuracyFeedback = accuracyFeedback; }
    
    public String getCreatedDate() { return createdDate; }
    public void setCreatedDate(String createdDate) { this.createdDate = createdDate; }
    
    /**
     * 获取占卜类型的中文名称
     */
    public String getTypeDisplayName() {
        if (type == null) return "未知";
        switch (type.toLowerCase()) {
            case "tarot": return "塔罗牌";
            case "plumblossom": return "梅花易数";
            case "coinoracle": return "金钱卦";
            case "lenormand": return "雷诺曼";
            default: return type;
        }
    }
    
    /**
     * 获取格式化的时间字符串
     */
    public String getFormattedTime() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM-dd HH:mm");
        return sdf.format(new java.util.Date(timestamp));
    }
    
    /**
     * 获取格式化的日期时间字符串
     */
    public String getFormattedDateTime() {
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new java.util.Date(timestamp));
    }
    
    /**
     * 检查是否包含指定标签
     */
    public boolean hasTag(String tag) {
        if (tags == null || tag == null) return false;
        return tags.contains(tag);
    }
    
    /**
     * 添加标签
     */
    public void addTag(String tag) {
        if (tag == null || tag.trim().isEmpty()) return;
        if (tags == null || tags.isEmpty()) {
            tags = tag.trim();
        } else if (!hasTag(tag)) {
            tags = tags + "," + tag.trim();
        }
    }
    
    @Override
    public String toString() {
        return "DivinationHistory{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", question='" + question + '\'' +
                ", timestamp=" + timestamp +
                ", isFavorite=" + isFavorite +
                ", tags='" + tags + '\'' +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        DivinationHistory that = (DivinationHistory) obj;
        return id == that.id;
    }
    
    @Override
    public int hashCode() {
        return Long.hashCode(id);
    }
}
